以一維陣列CQ(0 ~ n-1)表示一個環狀佇列
指標front指向佇列前端元素的前一個位置,指標rear則指向佇列尾端的元素,如下圖。
from collections import deque
##定義一8個元素環形佇列
cq = deque([11,22,33,44,55,66], maxlen=8) ##如下圖
cq.append(77)
cq.extend([88])
# [11,22,33,44,55,66,77,88]
print(cq.pop()) # [11,22,33,44,55,66,77] --> 88
# 旋轉指標
cq.rotate(-1)
# at this point you have [22,33,44,55,66,77,11]
print(cq.pop()) # [22,33,44,55,66,77] --> 11
print(cq.pop()) # [22,33,44,55,66] --> 77
print(cq.pop()) # [22,33,44,55] --> 66
print(cq.pop()) # [22,33,44] --> 55